import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
//工具栏溢出菜单示例
class PopupMenuEx extends StatefulWidget{

  @override
  State<StatefulWidget> createState() {
    return _PopupMenuExState();
  }
}


class Choice{
  const Choice({required this.title, required this.icon});
  final String title;
  final IconData icon;
}

const List<Choice> choices = const<Choice>[
  const Choice(title: 'Car', icon: Icons.directions_car),
  const Choice(title: 'Bicyle', icon: Icons.directions_bike),
  const Choice(title: 'Boat', icon: Icons.directions_boat),
  const Choice(title: 'Bus', icon: Icons.directions_bus),
  const Choice(title: 'Train', icon: Icons.directions_railway),
  const Choice(title: 'Walk', icon: Icons.directions_walk),
];

class ChoiceCard extends StatelessWidget{
  const ChoiceCard({Key? key, required this.choice}) : super(key:key);
  final Choice choice;

  @override
  Widget build(BuildContext context) {
    final TextStyle textStyle = Theme.of(context).textTheme.display1!;
    return Card(
      color: Colors.white,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Icon(choice.icon, size: 128.0, color: textStyle.color,),
          Text(choice.title, style: textStyle,),
        ],
      ),
    );
  }
}

class _PopupMenuExState extends State<PopupMenuEx>
{
  Choice _selectedChoice = choices[0];

  void _select(Choice choice){
    setState(() {
      _selectedChoice = choice;
    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Basic AppBar'),
        actions: [
          IconButton(onPressed: (){_select(choices[0]);}, icon: Icon(choices[0].icon)),
          IconButton(onPressed: (){_select(choices[1]);}, icon: Icon(choices[1].icon)),
          PopupMenuButton(itemBuilder: (BuildContext context){
            return choices.skip(2).map((Choice choice){
              return PopupMenuItem(child: Text(choice.title),
              value: choice,);
            },).toList();
          },
          onSelected: _select,)
        ],
      ),
      body: Padding(padding: const EdgeInsets.all(16.0),
      child: Center(
    child: ChoiceCard(choice: _selectedChoice,),),
    ),
    );
  }
}